// mobile bind
// ============

define(['zepto', 'sui', 'utils'], function($, sui, utils){
  return {
  
    init: function(data) {
      this.data = data;
      this.bindSubmitAction();
    },
    
    bindSubmitAction: function() {
      
      var that = this;
      var submitButton = $("a.button-submit");
      
      submitButton.on('click', function(e) {
        e.preventDefault();
        
        var result = that.validate();
        
        if (result) {
          that.submitTarget();
        }
        
      });
      
    },
    
    validate: function() {
      
      var shortTarget = $('textarea[name="shortTarget"]').val().trim();
      var longTarget = $('textarea[name="longTarget"]').val().trim();
      
      if (!shortTarget) {
        $.toast('本阶段的数据目标不能为空');
        return false;
      }
      
      if (!longTarget) {
        $.toast('长期目标不能为空');
        return false;
      }
      
      return true;
      
    },
    
    submitTarget: function() {
      
      var that = this;
      
      $.showPreloader('正在提交');
      
      var processedData = {
          'shortTarget': $('textarea[name="shortTarget"]').val().trim(),
          'longTarget': $('textarea[name="longTarget"]').val().trim()
      }
      
      $.ajax({
        type: 'POST',
        url: window.__CTX__.root + '/profile/courses/' + that.data.roundId + '/targets',
        data: processedData
      }).done(function(response) {
        $.hidePreloader();
        utils.processResult(response, function(result) {
          $.toast('填写学员目标成功');
          setTimeout(function() {
            window.location.href = that.data.redirect;
          }, 1000)
        }, function(result) {
          $.toast(result.message || '填写学员目标失败')
        })
      })
      
    }
  
  }
})

